package cn.xuan.scan.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.xuan.pojo.SysPermissions;
import cn.xuan.pojo.SysRoles;
import cn.xuan.pojo.SysRolesPermissions;
import cn.xuan.pojo.SysUsers;
import cn.xuan.pojo.SysUsersRoles;
import cn.xuan.pojo.SysUsersRolesId;
import cn.xuan.scan.dao.CommonQueryDAO;
import cn.xuan.scan.dao.query.param.CommonQueryParam;
import cn.xuan.scan.dao.query.param.QueryExpressionUtil;
import cn.xuan.scan.service.Userservice;

@Service
public class UserserviceImpl implements Userservice {
	
	@Autowired
	private CommonQueryDAO commonQueryDAO;

	@Override
	public List<SysRoles> findRoles(String username) throws Exception {
		SysUsers user = this.findByUsername(username);
		
		
		List<SysUsersRoles> usersRoles =  getUserRoles(user);
		List<Long> roleids = new ArrayList<Long>();
		for (SysUsersRoles usersRole : usersRoles) {
			roleids.add(usersRole.getId().getRoleId());
		}
		
		CommonQueryParam queryParam = new CommonQueryParam();
		
		queryParam.setEntityClass(SysRoles.class);
		queryParam.addExpression(QueryExpressionUtil.in("roleid", roleids ));
		
		return commonQueryDAO.queryObjects(queryParam);
		
	}

	@Override
	public List<SysPermissions> findPermissions(String username) throws Exception {
		List<SysRoles> roles = this.findRoles(username);
		
		List<SysRolesPermissions> rolesPermissions=  getRolesPermissions(roles);
		List<Long> permissionids = new ArrayList<Long>();
		for (SysRolesPermissions sysRolesPermissions : rolesPermissions) {
			permissionids.add(sysRolesPermissions.getId().getPermissionId());
		}
		
		CommonQueryParam queryParam = new CommonQueryParam();
		
		queryParam.setEntityClass(SysPermissions.class);
		queryParam.addExpression(QueryExpressionUtil.in("permissionid", permissionids ));
		
		return commonQueryDAO.queryObjects(queryParam);
		
	}

	







	@Override
	public SysUsers findByUsername(String username) throws Exception {
		CommonQueryParam queryParam = new CommonQueryParam();
		
		queryParam.setEntityClass(SysUsers.class);
		
		queryParam.addExpression(QueryExpressionUtil.equals("username", username));
		
		SysUsers users = (SysUsers) commonQueryDAO.queryObject(queryParam);
		
		return users;
	}
	
	private List<SysUsersRoles> getUserRoles(SysUsers user) throws Exception {
		List<SysUsersRoles> userRoles = new ArrayList<SysUsersRoles>();
		
		if(user != null) {
			CommonQueryParam queryParam = new CommonQueryParam();
			
			queryParam.setEntityClass(SysUsersRoles.class);
			
			queryParam.addExpression(QueryExpressionUtil.equals("id.userId", user.getId()));
			
			userRoles = commonQueryDAO.queryObjects(queryParam);
		}
		
		return userRoles;
	}
	
	private List<SysRolesPermissions> getRolesPermissions(List<SysRoles> roles) throws Exception {
		List<SysRolesPermissions>  rolesPermissions = new ArrayList<SysRolesPermissions>();
		
		if(roles != null && roles.size() > 0) {
			List<Long> roleids = new ArrayList<Long>();
			for (SysRoles role : roles) {
				roleids.add(role.getRoleid());
			}
			CommonQueryParam queryParam = new CommonQueryParam();
			
			queryParam.setEntityClass(SysRolesPermissions.class);
			
			queryParam.addExpression(QueryExpressionUtil.in("id.roleId", roleids));
			
			rolesPermissions = commonQueryDAO.queryObjects(queryParam);
		}
		
		return rolesPermissions;
	}
	
	
	/*private List<SysUsersRolesId> getUserRoles(SysUsers user) throws Exception {
		List<SysUsersRolesId> userRoles = new ArrayList<SysUsersRolesId>();
		if(user != null) {
			CommonQueryParam queryParam = new CommonQueryParam();
			
			queryParam.setEntityClass(SysUsersRolesId.class);
			
			queryParam.addExpression(QueryExpressionUtil.equals("userId", user.getId()));
			
			userRoles = commonQueryDAO.queryObjects(queryParam);
		}
		return userRoles;
	}
*/
}
